﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media.Imaging;
using Microsoft.Forums.ForumsClient.Models;
using Microsoft.Forums.OfflineStore;


namespace Microsoft.Forums.ForumsClient.Views
{
    /// <summary>
    ///  Gets the correct image for the post given its status and type
    /// </summary>
    public class ThreadPostStatusConverter : IMultiValueConverter
    {
        BitmapImage commentImage;
        BitmapImage answeredImage;
        BitmapImage proposedAnswerImage;
        BitmapImage questionImage;

        public ThreadPostStatusConverter()
        {
            commentImage = new BitmapImage(new Uri("Images\\Comment.png", UriKind.RelativeOrAbsolute));
            answeredImage = new BitmapImage(new Uri("Images\\Answered.png", UriKind.RelativeOrAbsolute));
            proposedAnswerImage = new BitmapImage(new Uri("Images\\ProposedAnswer.png", UriKind.RelativeOrAbsolute));
            questionImage = new BitmapImage(new Uri("Images\\Question.png", UriKind.RelativeOrAbsolute));
        }

        #region IMultiValueConverter Members

        /// <summary>
        /// Converts source values to a value for the binding target. The data binding engine calls this method when it propagates the values from source bindings to the binding target.
        /// </summary>
        /// <param name="values">The array of values that the source bindings in the <see cref="T:System.Windows.Data.MultiBinding"/> produces. The value <see cref="F:System.Windows.DependencyProperty.UnsetValue"/> indicates that the source binding has no value to provide for conversion.</param>
        /// <param name="targetType">The type of the binding target property.</param>
        /// <param name="parameter">The converter parameter to use.</param>
        /// <param name="culture">The culture to use in the converter.</param>
        /// <returns>
        /// A converted value.If the method returns null, the valid null value is used.A return value of <see cref="T:System.Windows.DependencyProperty"/>.<see cref="F:System.Windows.DependencyProperty.UnsetValue"/> indicates that the converter did not produce a value, and that the binding will use the <see cref="P:System.Windows.Data.BindingBase.FallbackValue"/> if it is available, or else will use the default value.A return value of <see cref="T:System.Windows.Data.Binding"/>.<see cref="F:System.Windows.Data.Binding.DoNothing"/> indicates that the binding does not transfer the value or use the <see cref="P:System.Windows.Data.BindingBase.FallbackValue"/> or the default value.
        /// </returns>
        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            ThreadedPostViewModel postModel = values.SingleOrDefault(v => (v is ThreadedPostViewModel)) as ThreadedPostViewModel;

            if (postModel == null) return null;

            BitmapImage outputImage = null;
            // Check if this is a close root post which means we show thread information
            if (postModel.Post.ParentId == null && !postModel.AreChildrenDisplayed)
            {
                ThreadType threadType = (ThreadType)postModel.ThreadIfRootPost.ThreadType;
                switch (threadType)
                {
                    case ThreadType.Comment:
                        outputImage = commentImage;
                        break;
                    case ThreadType.Question:
                        if ((bool)postModel.ThreadIfRootPost.IsAnswered)
                        {
                            outputImage = answeredImage;
                        }
                        else if ((bool)postModel.ThreadIfRootPost.HasProposedAnswers)
                        {
                            outputImage = proposedAnswerImage;
                        }
                        else
                        {
                            outputImage = questionImage;
                        }
                        break;
                    default:
                        break;
                }
            }
            else
            {
                if (postModel.Post.IsAnswer)
                {
                    outputImage = answeredImage; 
                }
                else
                {
                    PostType postType = (PostType)postModel.Post.PostType;
                    switch (postType)
                    {
                        case PostType.Answer:
                            outputImage = answeredImage;
                            break;
                        case PostType.ProposedAnswer:
                            outputImage = proposedAnswerImage;
                            break;
                        case PostType.Question:
                            outputImage = questionImage;
                            break;
                        default:
                            break;
                    }
                }
            }

            return outputImage;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}